package com.scan.bus.util;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.util.Set;

/**
 * Created by zhang on 2017/4/14 11 : 43.
 */

public class SharedPrefUtil {

    public static final String config = "buscar_config";
    public static final String buscar_cache = "buscar_cache";//缓存
    public static final String buscar_cache_complaint = "buscar_cache_complaint_new";//缓存投诉
    public static final String COLLECTION_GOODS_HISTORY = "collection_goods_";//收藏搜索商品历史
    public static final String COLLECTION_STORE_HISTORY = "collection_shop_";//收藏搜索店铺历史

    public static final String config_user = "buscar_user";

    public static SharedPreferences getsharedPreferences(){
        return Common.getApplicationContext().getSharedPreferences(config, Context.MODE_PRIVATE);
    }

    public static SharedPreferences  getsharedPreferencesUser(){
        String invite_code = SharedPrefUtil.getSharedUserString("invite_code", "");
        return Common.getApplicationContext().getSharedPreferences(config_user+invite_code, Context.MODE_PRIVATE);
    }

    public static SharedPreferences getsharedPreferences(String file_name){
        return Common.getApplicationContext().getSharedPreferences(file_name, Context.MODE_PRIVATE);
    }

    /**
     * 从sp中获取string
     * @param key
     * @param def
     * @return
     */
    public static Object getSharedUserDbData(String key, Object def){
        if(def instanceof String){
            return getsharedPreferencesUser().getString(key, (String) def);
        }else if(def instanceof Integer){
            return getsharedPreferencesUser().getInt(key, (Integer) def);
        }else if(def instanceof Boolean){
            return getsharedPreferencesUser().getBoolean(key, (boolean) def);
        }
        return "";
    }

    /**
     * 将string保存到sp中
     * @param key
     * @param value
     * @return
     */
    public static boolean saveSharedUserDbData(String key, Object value){
        if(!TextUtils.isEmpty(key)&&key.equals("invite_code")){
//            App.setTrackerUserId();
        }
        if(value instanceof String){
           return getsharedPreferencesUser().edit().putString(key, (String) value).commit();
        }else if(value instanceof Integer){
           return getsharedPreferencesUser().edit().putInt(key, (Integer) value).commit();
        }else if(value instanceof Boolean){
           return getsharedPreferencesUser().edit().putBoolean(key, (boolean) value).commit();
        }
        return false;
    }

    /**
     * 从sp中获取boolean
     * @param key
     * @param def
     * @return
     */
    public static boolean getCacheSharedPrfBoolean(String key, boolean def){
        return getsharedPreferences(buscar_cache).getBoolean(key,def);
    }

    /**
     * 将boolean保存到sp中
     * @param key
     * @param value
     * @return
     */
    public static boolean saveCacheSharedPrfBoolean(String key, boolean value){
        return getsharedPreferences(buscar_cache).edit().putBoolean(key,value).commit();
    }

    /**
     * 从sp中获取long
     * @param key
     * @param def
     * @return
     */
    public static long getCacheSharedPrfLong(String key, long def){
        return getsharedPreferences(buscar_cache).getLong(key,def);
    }

    /**
     * 将long保存到sp中
     * @param key
     * @param value
     * @return
     */
    public static boolean saveCacheSharedPrfLong(String key, long value){
        return getsharedPreferences(buscar_cache).edit().putLong(key,value).commit();
    }

    /**
     * 从sp中获取string
     * @param key
     * @param def
     * @return
     */
    public static String getSharedUserString(String key, String def){
       return getsharedPreferences().getString(key,def);
    }

    /**
     * 将string保存到sp中
     * @param key
     * @param value
     * @return
     */
    public static boolean saveSharedUserString(String key, String value){
        if(!TextUtils.isEmpty(key)&&key.equals("invite_code")){
//            App.setTrackerUserId();
        }
         return getsharedPreferences().edit().putString(key,value).commit();
    }

    /**
     * 将boolean保存到sp中
     * @param key
     * @param value
     * @return
     */
    public static boolean saveSharedUserBoolean(String key, boolean value){
        return getsharedPreferences().edit().putBoolean(key,value).commit();
    }

    /**
     * 从sp中获取long
     * @param key
     * @param def
     * @return
     */
    public static boolean getSharedUserBoolean(String key, boolean def){
        return getsharedPreferences().getBoolean(key,def);
    }

    /**
     * 从sp中获取long
     * @param key
     * @param def
     * @return
     */
    public static long getSharedUserLong(String key, long def){
        return getsharedPreferences().getLong(key,def);
    }

    /**
     * 将long保存到sp中
     * @param key
     * @param value
     * @return
     */
    public static boolean saveSharedUserLong(String key, long value){
        return getsharedPreferences().edit().putLong(key,value).commit();
    }

    /**
     * 将Set<String>保存到sp中
     * @param key
     * @param strings
     * @return
     */
    public static boolean saveSharedUserStringss(String key, Set<String> strings){
         return getsharedPreferences().edit().putStringSet(key,strings).commit();
    }


    /**
     * 从sp中获取Set<String>
     * @param key
     * @param strings
     * @return
     */
    public static Set<String> getSharedUserStringss(String key, Set<String> strings){
        return getsharedPreferences().getStringSet(key,strings);
    }
    /**
     * 清空sp文件内容
     */
    public static void clearSharedPreferences(){
        getsharedPreferences().edit().clear().commit();
    }


    /**
     * 从sp中获取string
     * @param key
     * @param def
     * @return
     */
    public static String getCacheSharedPrf(String key,String def){
        return getsharedPreferences(buscar_cache).getString(key,def);
}

    /**
     * 将string保存到sp中
     * @param key
     * @param value
     * @return
     */
    public static boolean saveCacheSharedPrf(String key,String value){
        return getsharedPreferences(buscar_cache).edit().putString(key,value).commit();
    }

    /**
     * 将Set<String>保存到sp中
     * @param key
     * @param strings
     * @return
     */
    public static boolean saveCacheSharedPrfStringss(String key,Set<String> strings){
        return getsharedPreferences(buscar_cache).edit().putStringSet(key,strings).commit();
    }

    /**
     * 从sp中获取Set<String>
     * @param key
     * @param strings
     * @return
     */
    public static Set<String> getCacheSharedPrfStringss(String key, Set<String> strings){
        return getsharedPreferences(buscar_cache).getStringSet(key,strings);
    }
    /**
     * 从sp中获取string
     * @param key
     * @param def
     * @return
     */
    public static Integer getCacheSharedPrfInt(String key,int def){
        return getsharedPreferences(buscar_cache).getInt(key,def);
}

    /**
     * 将string保存到sp中
     * @param key
     * @param value
     * @return
     */
    public static boolean saveCacheSharedPrfInt(String key,int value){
        return getsharedPreferences(buscar_cache).edit().putInt(key,value).commit();
    }

    /**
     * 清空sp文件内容
     */
    public static void clearCacheSharedPref(){
        getsharedPreferences(buscar_cache).edit().clear().commit();
    }

    /**
     * 跳转到微信
     */
    public static void getWechatApi(Activity context){
        try {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cmp);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            // TODO: handle exception
            Common.staticToast("检查到您手机没有安装微信，请安装后使用该功能");
        }
    }

    /**
     * 将string保存到sp中
     * @param tag 投诉的标签
     *  @param content   补充说明（必填）
     *  @param phone   手机号码（必填）
     *  @param buscar_link   顺联动力商品链接（必填）
     *  @param low_link   低价商品链接（必填）
     * @return
     */
    public static boolean saveCacheComplaintSharedPrf(String tag, String content,String phone ,String buscar_link,String low_link){
        return getsharedPreferences(buscar_cache_complaint)
                .edit()
                .putString("tag",tag)
                .putString("content",content)
                .putString("phone",phone)
                .putString("buscar_link",buscar_link)
                .putString("low_link",low_link)
                .commit();
    }

    /**
     * 清空sp文件内容
     */
    public static void clearCacheComplaintPref(){
        getsharedPreferences(buscar_cache_complaint).edit().clear().commit();
    }
}
